#include "input/Input.hh"

#include "common/stl.h"
#include "globals/keys.h"
#include "globals/engines.h"
#include "constants/default_keys.h"

#include "input/InputCharacter.hh"
#include "tools/Matrix.hh"

Input::Input() : _quit(false)
{
	// is this really a good idea ?
	glfwEnable(GLFW_STICKY_KEYS); // 'always disable STICKY_KEYS when leaving a section' [ex: menu]

	for (int i(0); i < NB_MOUSE_BUTTONS; ++i)
		_mouseButtons[i] = false;
}

Input::~Input()
{
    //dtor
}


bool Input::quit() const
{
  return (_quit);

  // should be if (_quit), do something

  // could also get event close && treat it
  // void glfwSetWindowCloseCallback( GLFWwindowclosefun cbfun )
}


// rename this && rewrite to enable a smoother quit
void Input::treatEvent()
{
	#ifdef DEBUG
		_quit = (glfwGetKey(GLFW_KEY_ESC) == GLFW_PRESS ||
			!glfwGetWindowParam(GLFW_OPENED));
	#else
		// in release, we're supposed to play on full-screen
		//   => no window close test
		_quit = glfwGetKey(GLFW_KEY_ESC) == GLFW_PRESS;
	#endif

	// shd set quit at true in menu, when u ask to quit 4 ex
}


void Input::keyboardCallback(int key, int state)
{
	// NOTE: if (state) is equivalent of testing state == GLFW_PRESS
	// NOTE: unfortunately, we cannot use a switch here (cases require constant expressions)

   if (key == g_key_move_forward && state)
		return InputCharacter::moveForward();

	if (key == g_key_move_backward && state)
		return InputCharacter::moveBackward();

	if (key == g_key_move_left && state)
		return InputCharacter::moveLeft();

	if (key == g_key_move_right && state)
		return InputCharacter::moveRight();

	if (key == g_key_tilt_left && state)
		return InputCharacter::tiltLeft();

	if (key == g_key_tilt_right && state)
		return InputCharacter::tiltRight();

	#ifdef DEBUG
		if (state)
			std::cout << "unknown key " << key << std::endl;
	#endif
}


void Input::mouseButtonsCallback(int button, int state)
{
	// TODO: play a sound on click, using \param state

	switch (button)
	{
	//	case GLFW_MOUSE_BUTTON_LEFT:
	//		Input::_mouseLeft = (state == GLFW_PRESS);
	//		return;

	//	case GLFW_MOUSE_BUTTON_RIGHT:
	//		_mouseRight = (state == GLFW_PRESS);
	//		return;
	//
	//	case GLFW_MOUSE_BUTTON_MIDDLE:
	//		_mouseMiddle = (state == GLFW_PRESS);
	//		return;
	}
}

// Updates mouse coordinates
void Input::mousePositionCallback(int x, int y)
{
    if (g_input_control == CONTROL_CHARACTER)
        glfwSetMousePosCallback(InputCharacter::mousePositionCallback);

//        i.look(x, y);
}
